﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HECore.Common.Redis
{
    /// <summary>
    /// Redis Client
    /// </summary>
    public class BaseRedisClient
    {
        private readonly string _connectionString;
        private readonly ConnectionMultiplexer _connectionMultiplexer;
        private readonly int _IndexDatabase;

        /// <summary>
        /// 框架Redis帮助类
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="password"></param>
        /// <param name="indexDatabase"></param>
        public BaseRedisClient(string ip = "127.0.0.1", int port = 6379, string password = "", int indexDatabase = 0)
        {
            _connectionString = $"{ip}:{port}";
            if (!string.IsNullOrEmpty(password)) { _connectionString += $",password={password}"; }

            _IndexDatabase = indexDatabase;
            _connectionMultiplexer = ConnectionMultiplexer.Connect(_connectionString);
        }
        /// <summary>
        /// 获取数据库
        /// </summary>
        /// <returns></returns>
        private IDatabase GetDatabase() { return _connectionMultiplexer.GetDatabase(_IndexDatabase); }
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose() { _connectionMultiplexer.Dispose(); }


        /// <summary>
        /// Set字符串,设置过期时间
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expir">过期时间</param>
        public void StringSetExpiry(string key, string value, TimeSpan expir)
        {
            GetDatabase().StringSet(key, value, expir);
        }
        /// <summary>
        /// Set字符串,设置过期时间
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expir">过期时间</param>
        public async Task StringSetExpiryAsync(string key, string value, TimeSpan expir)
        {
            await GetDatabase().StringSetAsync(key, value, expir);
        }

        /// <summary>
        /// Get字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string? StringGet(string key)
        {
            return GetDatabase().StringGet(key);
        }
        /// <summary>
        /// Get字符串
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<string?> StringGetAsync(string key)
        {
            return await GetDatabase().StringGetAsync(key);
        }

        /// <summary>
        /// 验证Key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool KeyExists(string key)
        {
            return GetDatabase().KeyExists(key);
        }
        /// <summary>
        /// 验证Key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<bool> KeyExistsAsync(string key)
        {
            return await GetDatabase().KeyExistsAsync(key);
        }

        /// <summary>
        /// 删除Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public void KeyDelete(string key)
        {
            GetDatabase().KeyDelete(key);
        }
        /// <summary>
        /// 删除Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task KeyDeleteAsync(string key)
        {
            await GetDatabase().KeyDeleteAsync(key);
        }
    }
}